package leetcode.method.dynamic_programming;

public class Program213RobTheHouse {

    public Program213RobTheHouse() {

    }

    public int rob(int[] nums) {
        int length = nums.length;
        if (length == 0) {
            return 0;
        }
        if (length == 1) {
            return nums[0];
        }

        return Math.max(robByIndex(nums,0, length - 2), robByIndex(nums, 1, length - 1));
    }

    public int robByIndex(int[] nums, int start, int end) {
        int length = nums.length;
        if (length - 1 == 1) {
            return nums[start];
        }

        int[][] dp = new int[length][2];
        dp[start][0] = 0;
        dp[start][1] = nums[start];

        for (int i = start + 1; i < length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
            dp[i][1] = dp[i - 1][0] + nums[i];
        }
        return Math.max(dp[end][0], dp[end][1]);
    }

    public static void main(String[] args) {
        Program213RobTheHouse myStack = new Program213RobTheHouse();
        int[] matrix = {2,3,2};
        System.out.println(myStack.rob(matrix));
        int[] matrix1 = {1,2,3,1};
        System.out.println(myStack.rob(matrix1));
        int[] matrix2 = {1,2,3};
        System.out.println(myStack.rob(matrix2));
    }
}
